#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        if (nums.size() == 0) return 0;
        int left = 0, right = 0;
        int sum = 0;
        int res = nums.size() + 1;

        while (right < nums.size()) {
            while (sum < s && right < nums.size())
                sum += nums[right++];
            while (left < right) {
                if (sum - nums[left] >= s) {
                    sum -= nums[left];
                    ++left;
                }
                else
                    break;
            }
            if (sum >= s) {
                res = min(res, right - left);
                sum -= nums[left++];
            }
        }
        return res == nums.size() + 1 ? 0 : res;
    }
};